package flc.ast.adapter;

import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import flc.ast.R;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.bean.StkResBean;

public class ReadRecordAdapter extends StkProviderMultiAdapter<StkResBean> {

    public ReadRecordAdapter() {
        addItemProvider(new StkSingleSpanProvider(126));
        addItemProvider(new ReadRecordProvider());
    }

    private class ReadRecordProvider extends BaseItemProvider<StkResBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_read_record;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, StkResBean bean) {
            Glide.with(getContext()).load(bean.getThumbUrl())
                    .into((ImageView) baseViewHolder.getView(R.id.ivReadRecordItemImg));
            baseViewHolder.setText(R.id.tvReadRecordItemName,bean.getName());
            baseViewHolder.setText(R.id.tvReadRecordItemDesc,bean.getDesc());
            baseViewHolder.setText(R.id.tvReadRecordItemTag1,bean.getTagNameList().get(1));
            baseViewHolder.setText(R.id.tvReadRecordItemTag2,bean.getTagNameList().get(2));
            try {
                Gson gson = new Gson();
                String json = gson.toJson(bean.getExtra());
                JSONObject jsonObject = new JSONObject(json);
                String releaseDate = jsonObject.getString("releaseDate");
                releaseDate=releaseDate.substring(0,4);
                baseViewHolder.setText(R.id.tvReadRecordItemTag3,releaseDate+getContext().getString(R.string.year));
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
